class Solution(object):
    def maxConsecutiveAnswers(self, answerKey, k):
        max_len = 0
        left = 0
        right = 0
        n = len(answerKey)
        t = 0
        f = 0
        while right < n:
            if answerKey[right] == 'F':
                f += 1
            else:
                t += 1
            while f > k:
                if answerKey[left] == 'F':
                    f -= 1
                else:
                    t -= 1
                left += 1
            max_len = max(max_len, right - left + 1)
            right += 1

        t = 0
        f = 0
        left = 0
        right = 0
        while right < n:
            if answerKey[right] == 'F':
                f += 1
            else:
                t += 1
            while t > k:
                if answerKey[left] == 'F':
                    f -= 1
                else:
                    t -= 1
                left += 1
            max_len = max(max_len, right - left + 1)
            right += 1

        return max_len